package is.model.dao;

import is.interfaces.Dao;
import is.model.bean.Employee;
import is.model.dao.search.EmployeeSearch;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author aspr0
 */
public class EmployeeDao extends AbstractDao<Employee> implements Dao<Employee>
{
    @Override
    public Integer create() throws SQLException
    {
        String query = "insert into %s %s";
        query = String.format(query, initialBean.getTableName(), genQmarksBycomma(initialBean));

        PreparedStatement statement = connection.prepareStatement(query);
        Integer parameterN = 1;

        statement.setString(parameterN++, initialBean.getLastName());
        statement.setString(parameterN++, initialBean.getFirstName());
        statement.setString(parameterN++, initialBean.getMiddleName());

        statement.setInt(parameterN++, initialBean.getDepartmentId());

        Integer telephone = initialBean.getTelephone();
        if (telephone != null)
        {
            statement.setInt(parameterN++, telephone);
        }
        statement.setFloat(parameterN++, initialBean.getSalary());

        statement.execute();
        return statement.getUpdateCount();
    }

    @Override
    public List<Employee> search(Boolean strict) throws SQLException
    {
        return EmployeeSearch.search(strict, initialBean, connection);
    }

    @Override
    public Integer update() throws SQLException
    {
        String query = "update %s set %s where id = ? limit 1";

        query = String.format(query, initialBean.getTableName(), genQmarksByeqsign(initialBean));

        PreparedStatement statement = connection.prepareStatement(query);

        Integer parameterN = 1;

        statement.setString(parameterN++, initialBean.getLastName());
        statement.setString(parameterN++, initialBean.getFirstName());
        statement.setString(parameterN++, initialBean.getMiddleName());

        statement.setInt(parameterN++, initialBean.getDepartmentId());

        Integer telephone = initialBean.getTelephone();
        if (telephone != null)
        {
            statement.setInt(parameterN++, telephone);
        }
        statement.setFloat(parameterN++, initialBean.getSalary());
        statement.setInt(parameterN++, initialBean.getId());


        statement.execute();

        return statement.getUpdateCount();
    }
}
